﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers.WebApi
{
    public class CarMaintainApiController : ApiController
    {
        private readonly Car_MaintainService service = new Car_MaintainService();
        /// <summary>
        /// 保存车辆维修数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object SaveCarMaintain()
        {
            Car_Maintain ent = new Car_Maintain();
            ent.CarId = Convert.ToInt32(HttpContext.Current.Request.Params["CarId"]);
            ent.DiverId = Convert.ToInt32(HttpContext.Current.Request.Params["DiverId"]);
            ent.MtOpinion = HttpContext.Current.Request.Params["MtOpinion"];
            decimal igMoney = 0; decimal.TryParse(HttpContext.Current.Request.Params["MtMoney"], out igMoney);
            ent.MtMoney = igMoney;
            ent.MtRemark = HttpContext.Current.Request.Params["MtRemark"];
            ent.MtTime = Convert.ToDateTime(HttpContext.Current.Request.Params["MtTime"]);
            ent.InstanceState = 0;
            ent.OrgzNo = Convert.ToInt32(HttpContext.Current.Request.Params["OrgzNo"]);
            ent.ToDoUrl = HttpContext.Current.Request.Params["ToDoUrl"];
            ent.UserName = HttpContext.Current.Request.Params["UserName"];
            ent.UserId = Convert.ToInt32(HttpContext.Current.Request.Params["UserId"]);
            ent.MtType = Convert.ToInt32(HttpContext.Current.Request.Params["MtType"]);
            ent.MtGrade = Convert.ToInt32(HttpContext.Current.Request.Params["MtGrade"]);
            var carState = Convert.ToInt32(HttpContext.Current.Request.Params["carState"]);

            #region 处理上传附件信息
            var fileCount = HttpContext.Current.Request.Files.Count;
            IList<Sys_Attachments> list = null;
            if (fileCount > 0)
            {
                if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/UploadFiles")))
                {
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/UploadFiles"));
                }
                list = new List<Sys_Attachments>();
                for (int i = 0; i < fileCount; i++)
                {
                    HttpPostedFile file = HttpContext.Current.Request.Files[i];
                    var filename = "/UploadFiles/" + Guid.NewGuid() + file.FileName;
                    string fileMapPath = HttpContext.Current.Server.MapPath(filename);
                    file.SaveAs(fileMapPath);
                    Sys_Attachments att = new Sys_Attachments();
                    att.FileDescription = string.Empty;
                    att.FileName = file.FileName;
                    att.FileUrl = filename;
                    att.ObjectName = "CMTS";
                    att.State = 0;
                    list.Add(att);
                }
            }
            #endregion
            var tmp = service.SaveData(ent, list,carState);
            JavaScriptSerializer json = new JavaScriptSerializer();
            return new HttpResponseMessage { Content = new StringContent(json.Serialize(tmp), System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取保险购买流程列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        [HttpPost]
        public object GetPageData()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            int pageSize = Convert.ToInt32(HttpContext.Current.Request["pageSize"]);
            int pageIndex = Convert.ToInt32(HttpContext.Current.Request["pageIndex"]);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var tmp = service.GetList(id, pageSize, pageIndex);
            var data = json.Serialize(tmp);
            if (tmp == null)
            {
                data = "[]";
            }
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetBaseItem()
        {
            int typeId = Convert.ToInt32(HttpContext.Current.Request["typeId"]);
            var list = new BaseItemService().GetListByType(typeId);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var data = json.Serialize(list);
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取事故申请明细
        /// </summary>
        /// <param name="id">业务ID</param>
        /// <returns></returns>
        [HttpPost]
        public object GetModel()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["bid"]);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var tmp = service.GetDetail(id);
            if (tmp == null) return "[]";
            var data = json.Serialize(tmp);
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 判断当前用户是否具有审核权限
        /// </summary>
        /// <returns>0表示可以审核;1、表示已经审核过</returns>
        [HttpPost]
        public object CurrUserIsApproal()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["bid"]);
            var ent = service.GetDetail(id);
            if (ent.InstanceState == 2)
            {
                return 1;
            }
            int userId = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            return CommService.IsApproval(ent.ApproalUsers, userId, ent.NodeCount);
        }
        /// <summary>
        /// 事故审批
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Approal()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["bid"]);
            int userId = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            int opa = Convert.ToInt32(HttpContext.Current.Request["opa"]);
            int todoId = Convert.ToInt32(HttpContext.Current.Request["todo"]);
            var opContext = HttpContext.Current.Request["opContext"];
            var tmp = service.Approal(id, userId, opa, todoId, opContext);
            JavaScriptSerializer json = new JavaScriptSerializer();
            return new HttpResponseMessage { Content = new StringContent(json.Serialize(tmp), System.Text.Encoding.UTF8, "application/json") };
        }
    }
}
